package cn.mihoyo.base.controller;

import cn.mihoyo.base.dto.EmailRegisterDto;
import cn.mihoyo.base.dto.PhoneRegisterDto;
import cn.mihoyo.base.service.VerifyCodeService;
import cn.mihoyo.base.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private VerifyCodeService verifyCodeService;

    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String imageCodeKey) throws IOException {
        String base64Str =verifyCodeService.imageCodeKey(imageCodeKey);
        return AjaxResult.me().setResultObj(base64Str);
    }
    @GetMapping("/email/image/{imageCodeKey}")
    public AjaxResult emailImageCode(@PathVariable("imageCodeKey") String imageCodeKey) throws IOException {
        String base64Str = verifyCodeService.emailImageCode(imageCodeKey);
        return AjaxResult.me().setResultObj(base64Str);
    }

    @PostMapping("/smsCode")
    public AjaxResult phoneRegister(@RequestBody @Valid PhoneRegisterDto dto){
        verifyCodeService.smsCode(dto);
        return AjaxResult.me();
    }
    @PostMapping("/emailSmsCode")
    public AjaxResult phoneRegister(@RequestBody @Valid EmailRegisterDto dto){
        verifyCodeService.EeailsmsCode(dto);
        return AjaxResult.me();
    }
}
